/**
 * <copyright>
 *
 * Copyright (c) 2011 Zeligsoft (2009) Limited
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   Zeligsoft (2009) Limited - Initial API and implementation
 *
 * </copyright>
 *
 */
package org.omg.deployment.components;

import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
import org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent;
import org.eclipse.emf.eef.runtime.context.PropertiesEditingContext;
import org.eclipse.emf.eef.runtime.impl.components.SinglePartPropertiesEditingComponent;
import org.eclipse.emf.eef.runtime.impl.utils.EEFConverterUtil;
import org.eclipse.emf.eef.runtime.ui.widgets.referencestable.ReferencesTableSettings;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.omg.deployment.ArtifactDeploymentDescription;
import org.omg.deployment.DeploymentPackage;
import org.omg.deployment.Property;
import org.omg.deployment.Requirement;
import org.omg.deployment.ResourceDeploymentDescription;
import org.omg.deployment.parts.ArtifactDeploymentDescriptionPropertiesEditionPart;
import org.omg.deployment.parts.DeploymentViewsRepository;


/**
 * @author Toby McClean; Zeligsoft (2009) Limited
 * @generated
 */
public class ArtifactDeploymentDescriptionBasePropertiesEditionComponent extends SinglePartPropertiesEditingComponent {

	/**
	 * @generated
	 */
	
	public static String BASE_PART = "Base"; //$NON-NLS-1$

	
	/**
	 * Settings for execParameter ReferencesTable
	 */
	protected ReferencesTableSettings execParameterSettings;
	
	/**
	 * Settings for deployRequirement ReferencesTable
	 */
	protected ReferencesTableSettings deployRequirementSettings;
	
	/**
	 * Settings for deployedResource ReferencesTable
	 */
	protected ReferencesTableSettings deployedResourceSettings;
	
	
	/**
	 * Default constructor
	 * @generated
	 */
	public ArtifactDeploymentDescriptionBasePropertiesEditionComponent(PropertiesEditingContext editingContext, EObject artifactDeploymentDescription, String editing_mode) {
    super(editingContext, artifactDeploymentDescription, editing_mode);
    parts = new String[] { BASE_PART };
    repositoryKey = DeploymentViewsRepository.class;
    partKey = DeploymentViewsRepository.ArtifactDeploymentDescription.class;
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.runtime.api.component.IPropertiesEditionComponent#initPart(java.lang.Object, int, org.eclipse.emf.ecore.EObject, 
	 *      org.eclipse.emf.ecore.resource.ResourceSet)
	 * @generated
	 */
	public void initPart(Object key, int kind, EObject elt, ResourceSet allResource) {
    setInitializing(true);
    if (editingPart != null && key == partKey) {
      editingPart.setContext(elt, allResource);
      final ArtifactDeploymentDescription artifactDeploymentDescription = (ArtifactDeploymentDescription)elt;
      final ArtifactDeploymentDescriptionPropertiesEditionPart basePart = (ArtifactDeploymentDescriptionPropertiesEditionPart)editingPart;
      // init values
      if (artifactDeploymentDescription.getName() != null && isAccessible(DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.name))
        basePart.setName(EEFConverterUtil.convertToString(XMLTypePackage.eINSTANCE.getString(), artifactDeploymentDescription.getName()));
      
      // FIXME NO VALID CASE INTO template public updater(editionElement : PropertiesEditionElement, view : View, pec : PropertiesEditionComponent) in widgetControl.mtl module, with the values : source, ArtifactDeploymentDescription, ArtifactDeploymentDescription.
      // FIXME NO VALID CASE INTO template public updater(editionElement : PropertiesEditionElement, view : View, pec : PropertiesEditionComponent) in widgetControl.mtl module, with the values : location, ArtifactDeploymentDescription, ArtifactDeploymentDescription.
      if (artifactDeploymentDescription.getNode() != null && isAccessible(DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.node))
        basePart.setNode(EEFConverterUtil.convertToString(XMLTypePackage.eINSTANCE.getString(), artifactDeploymentDescription.getNode()));
      
      if (isAccessible(DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.ExecParameters.execParameter)) {
        execParameterSettings = new ReferencesTableSettings(artifactDeploymentDescription, DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_ExecParameter());
        basePart.initExecParameter(execParameterSettings);
      }
      if (isAccessible(DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.Requirements.deployRequirement)) {
        deployRequirementSettings = new ReferencesTableSettings(artifactDeploymentDescription, DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_DeployRequirement());
        basePart.initDeployRequirement(deployRequirementSettings);
      }
      if (isAccessible(DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.Resources.deployResource)) {
        deployedResourceSettings = new ReferencesTableSettings(artifactDeploymentDescription, DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_DeployedResource());
        basePart.initDeployResource(deployedResourceSettings);
      }
      // init filters
      
      // FIXME NO VALID CASE INTO template public filterUpdater(editionElement : PropertiesEditionElement, view : View, pec : PropertiesEditionComponent) in widgetControl.mtl module, with the values : source, ArtifactDeploymentDescription, ArtifactDeploymentDescription.
      // FIXME NO VALID CASE INTO template public filterUpdater(editionElement : PropertiesEditionElement, view : View, pec : PropertiesEditionComponent) in widgetControl.mtl module, with the values : location, ArtifactDeploymentDescription, ArtifactDeploymentDescription.
      
      basePart.addFilterToExecParameter(new ViewerFilter() {
      
          /**
           * {@inheritDoc}
           * 
           * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
           */
          public boolean select(Viewer viewer, Object parentElement, Object element) {
            return (element instanceof String && element.equals("")) || (element instanceof Property); //$NON-NLS-1$ 
          }
      
      });
      
      basePart.addFilterToDeployRequirement(new ViewerFilter() {
      
          /**
           * {@inheritDoc}
           * 
           * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
           */
          public boolean select(Viewer viewer, Object parentElement, Object element) {
            return (element instanceof String && element.equals("")) || (element instanceof Requirement); //$NON-NLS-1$ 
          }
      
      });
      
      basePart.addFilterToDeployResource(new ViewerFilter() {
      
          /**
           * {@inheritDoc}
           * 
           * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
           */
          public boolean select(Viewer viewer, Object parentElement, Object element) {
            return (element instanceof String && element.equals("")) || (element instanceof ResourceDeploymentDescription); //$NON-NLS-1$ 
          }
      
      });
      
      // init values for referenced views
      
      // init filters for referenced views
      
    }
    setInitializing(false);
  }










	/**
	 * {@inheritDoc}
	 * @see org.eclipse.emf.eef.runtime.impl.components.StandardPropertiesEditionComponent#associatedFeature(java.lang.Object)
	 */
	public EStructuralFeature associatedFeature(Object editorKey) {
		if (editorKey == DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.name) {
			return DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_Name();
		}
		if (editorKey == DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.source) {
			return DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_Source();
		}
		if (editorKey == DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.location) {
			return DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_Location();
		}
		if (editorKey == DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.node) {
			return DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_Node();
		}
		if (editorKey == DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.ExecParameters.execParameter) {
			return DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_ExecParameter();
		}
		if (editorKey == DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.Requirements.deployRequirement) {
			return DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_DeployRequirement();
		}
		if (editorKey == DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.Resources.deployResource) {
			return DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_DeployedResource();
		}
		return super.associatedFeature(editorKey);
	}

	/**
	 * {@inheritDoc}
	 * @see org.eclipse.emf.eef.runtime.impl.components.StandardPropertiesEditionComponent#updateSemanticModel(org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent)
	 * @generated
	 */
	public void updateSemanticModel(final IPropertiesEditionEvent event) {
    ArtifactDeploymentDescription artifactDeploymentDescription = (ArtifactDeploymentDescription)semanticObject;
  }

	/**
	 * {@inheritDoc}
	 * @see org.eclipse.emf.eef.runtime.impl.components.StandardPropertiesEditionComponent#updatePart(org.eclipse.emf.common.notify.Notification)
	 */
	public void updatePart(Notification msg) {
		if (editingPart.isVisible()) {	
			ArtifactDeploymentDescriptionPropertiesEditionPart basePart = (ArtifactDeploymentDescriptionPropertiesEditionPart)editingPart;
			if (DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_Name().equals(msg.getFeature()) && basePart != null && isAccessible(DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.name)) {
				if (msg.getNewValue() != null) {
					basePart.setName(EcoreUtil.convertToString(XMLTypePackage.eINSTANCE.getString(), msg.getNewValue()));
				} else {
					basePart.setName("");
				}
			}
			// FIXME INVALID CASE INTO template public liveUpdater(editionElement : PropertiesEditionElement, view : View, pec : PropertiesEditionComponent) in widgetControl.mtl module, with the values : source, ArtifactDeploymentDescription, ArtifactDeploymentDescription.
			// FIXME INVALID CASE INTO template public liveUpdater(editionElement : PropertiesEditionElement, view : View, pec : PropertiesEditionComponent) in widgetControl.mtl module, with the values : location, ArtifactDeploymentDescription, ArtifactDeploymentDescription.
			if (DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_Node().equals(msg.getFeature()) && basePart != null && isAccessible(DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.node)) {
				if (msg.getNewValue() != null) {
					basePart.setNode(EcoreUtil.convertToString(XMLTypePackage.eINSTANCE.getString(), msg.getNewValue()));
				} else {
					basePart.setNode("");
				}
			}
			if (DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_ExecParameter().equals(msg.getFeature()) && isAccessible(DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.ExecParameters.execParameter))
				basePart.updateExecParameter();
			if (DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_DeployRequirement().equals(msg.getFeature()) && isAccessible(DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.Requirements.deployRequirement))
				basePart.updateDeployRequirement();
			if (DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_DeployedResource().equals(msg.getFeature()) && isAccessible(DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.Resources.deployResource))
				basePart.updateDeployResource();
			
		}
	}


	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.runtime.api.component.IPropertiesEditionComponent#validateValue(org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent)
	 * @generated
	 */
	public Diagnostic validateValue(IPropertiesEditionEvent event) {
    Diagnostic ret = Diagnostic.OK_INSTANCE;
    if (event.getNewValue() != null) {
      try {
        if (DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.name == event.getAffectedEditor()) {
          Object newValue = event.getNewValue();
          if (newValue instanceof String) {
            newValue = EcoreUtil.createFromString(DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_Name().getEAttributeType(), (String)newValue);
          }
          ret = Diagnostician.INSTANCE.validate(DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_Name().getEAttributeType(), newValue);
        }
        if (DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.source == event.getAffectedEditor()) {
          BasicDiagnostic chain = new BasicDiagnostic();
          for (Iterator iterator = ((List)event.getNewValue()).iterator(); iterator.hasNext();) {
            chain.add(Diagnostician.INSTANCE.validate(DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_Source().getEAttributeType(), iterator.next()));
          }
          ret = chain;
        }
        if (DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.location == event.getAffectedEditor()) {
          BasicDiagnostic chain = new BasicDiagnostic();
          for (Iterator iterator = ((List)event.getNewValue()).iterator(); iterator.hasNext();) {
            chain.add(Diagnostician.INSTANCE.validate(DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_Location().getEAttributeType(), iterator.next()));
          }
          ret = chain;
        }
        if (DeploymentViewsRepository.ArtifactDeploymentDescription.Properties.node == event.getAffectedEditor()) {
          Object newValue = event.getNewValue();
          if (newValue instanceof String) {
            newValue = EcoreUtil.createFromString(DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_Node().getEAttributeType(), (String)newValue);
          }
          ret = Diagnostician.INSTANCE.validate(DeploymentPackage.eINSTANCE.getArtifactDeploymentDescription_Node().getEAttributeType(), newValue);
        }
      } catch (IllegalArgumentException iae) {
        ret = BasicDiagnostic.toDiagnostic(iae);
      } catch (WrappedException we) {
        ret = BasicDiagnostic.toDiagnostic(we);
      }
    }
    return ret;
  }

}
